package com.dsteiner.widgets.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.IJsUtil;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.Widgets.AbsolutePanelExt;
import com.dsteiner.widgets.client.Widgets.FlowPanelExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.IAbsolutePanel;
import com.dsteiner.widgets.client.Widgets.IFlowPanel;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.IImage;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.ISimplePanel;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.SimplePanelExt;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.event.Event;
import com.dsteiner.widgets.client.event.EventListener;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.event.WidgetSizeChangeEvent;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class PopupOverlayController {
	
	static int topOffset = 50;
	public static void showAtCenter(PopupOverlayModel popupOverlayModel) {
		popupOverlayModel.setVisible(true);
		int clientWidth = JsUtil.getInstance().getClientWidth();
		int clientHeight = JsUtil.getInstance().getClientHeight();
		IWidget contentWidget = popupOverlayModel.getContentWidget();
		int offsetHeight = contentWidget.getOffsetHeight();
		int offsetWidth = contentWidget.getOffsetWidth();
		int scrollTop = JsUtil.getInstance().getScrollTop();
		popupOverlayModel.setLeft(clientWidth/2 - (offsetWidth > 0 ? offsetWidth/2 : 300));
		popupOverlayModel.setTop(scrollTop + topOffset);
	}
	private PopupOverlayModel popupOverlayModel;
	private IFlowPanel mainPanel;
	private ILazyWidget popupOverlayWidget;
	private IWidget glassPane;
	private ISimplePanel contentPanel;
	private IJsUtil jsUtil;

	private IHTML closeLink;

	public void registerCloseIcon(IImage closeIcon) {
		closeIcon.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				popupOverlayModel.setVisible(false);
			}
		});
	}

	public void registerCloseLink(IHTML closeLink) {
		this.closeLink = closeLink;
		closeLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				popupOverlayModel.setVisible(false);
			}
		});
	}

	public void registerContentPanel(ISimplePanel contentPanel) {
		this.contentPanel = contentPanel;
	}

	public void registerEventManager(EventManager eventManager) {
//		eventManager.registerListener(WidgetSizeChangeEvent.class, new EventListener<WidgetSizeChangeEvent>(){
//			public void onEvent(WidgetSizeChangeEvent event) {
//				Widget widget = event.getWidget();
//				IWidget contentWidget = popupOverlayModel.getContentWidget();
//				if(widget == contentWidget && popupOverlayModel.isVisible()){
//					DeferredCommand.addCommand(new Command(){
//						public void execute() {
//							showAtCenter(popupOverlayModel);
//						}
//					});
//				}
//			}
//		});
	}

	public void registerJsUtil(IJsUtil jsUtil) {
		this.jsUtil = jsUtil;
		jsUtil.addWindowResizeHandler(new ResizeHandler(){
			public void onResize(ResizeEvent event) {
				updateMaxHeight();
			}
		});
//		jsUtil.addWindowScrollHandler(new Window.ScrollHandler(){
//			public void onWindowScroll(com.google.gwt.user.client.Window.ScrollEvent event) {
//				updatePosition();
//			}
//		});
	}

	public void registerMainPanel(IFlowPanel mainPanel) {
		this.mainPanel = mainPanel;
	}

	public void registerGlassPane(IWidget glassPane) {
		this.glassPane = glassPane;
	}

	public void registerPopupOverlayModel(PopupOverlayModel popupOverlayModel) {
		this.popupOverlayModel = popupOverlayModel;
		popupOverlayModel.addPropertyChangeListener("contentWidget", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateContentWidget();
			}
		});
		popupOverlayModel.addPropertyChangeListener("top", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updatePosition();
			}
		});
		popupOverlayModel.addPropertyChangeListener("left", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updatePosition();
			}
		});
		popupOverlayModel.addPropertyChangeListener("visible", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateVisibility();
			}
		});
		popupOverlayModel.addPropertyChangeListener("showCloseLink", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateCloseLinkVisibility();
			}
		});
		
	}

	public void registerPopupOverlayWidget(ILazyWidget popupOverlayWidget) {
		this.popupOverlayWidget = popupOverlayWidget;
		popupOverlayWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateContentWidget();
				updateVisibility();
				updatePosition();
				updateCloseLinkVisibility();
				updateMaxHeight();
				glassPane.setVisible(true);
			}
			@Override
			public void onDetach(IWidget widget) {
				glassPane.setVisible(false);
			}
		});
	}

	private void updateCloseLinkVisibility() {
		closeLink.setVisible(popupOverlayModel.getShowCloseLink());
	}

	private void updateContentWidget() {
		IWidget contentWidget = popupOverlayModel.getContentWidget();
		if(contentWidget == null || contentPanel == null)
			return;
		contentPanel.setWidget(contentWidget);
	}

	private void updateMaxHeight() {
		int clientHeight = jsUtil.getClientHeight();
		int maxHeight = clientHeight-topOffset-40;
		jsUtil.setStyleAttribute(contentPanel, "maxHeight", maxHeight+"px");
	}

	private void updatePosition() {
		int left = popupOverlayModel.getLeft();
		int top = popupOverlayModel.getTop();
		JsUtil.getInstance().setStyleAttribute(popupOverlayWidget, "left", left+"px");
		JsUtil.getInstance().setStyleAttribute(popupOverlayWidget, "top", top+"px");
		JsUtil.getInstance().setStyleAttribute(popupOverlayWidget, "position", "absolute");
	}

	private void updateVisibility() {
		boolean visible = popupOverlayModel.isVisible();
		if(visible && (popupOverlayWidget == null || !popupOverlayWidget.isAttached())){
			RootPanel.get().add((Widget) popupOverlayWidget);
		}
		else if(!visible){
			popupOverlayWidget.removeFromParent();
		}
		
	}
}